	/**
 * 
 */
package edu.neelsys.mvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import edu.neelsys.business.userauth.UserService;
import edu.neelsys.mvc.util.Constants;
import edu.neelsys.mvc.util.ErrorMessage;
import edu.neelsys.objectmodel.dbentites.User;
import edu.neelsys.objectmodel.userauth.LoggedUser;


public class UpdateUserController extends SimpleFormController {
	
	private UserService userService;

	public ModelAndView 
			onSubmit(HttpServletRequest request,HttpServletResponse response, Object command, BindException errors) throws Exception {
		
		LoggedUser loggedUser = (LoggedUser) SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal();
		
		//LoginForm form = (LoginForm) object;
		String userID= request.getParameter("userID");
		String auth=request.getParameter("auth");
		System.out.println("update Controller :"+auth);
		String userName = request.getParameter("username");
		String firstName=request.getParameter("firstname");
		String mname=request.getParameter("middlename");
		String lastName=request.getParameter("lastname");
		String email=request.getParameter("email");
		String pbirth=request.getParameter("placeofbirth");
		String marital=request.getParameter("maritalstatus");
		String national=request.getParameter("nationality");
		String mothername=request.getParameter("mothersmaidenname");
		String adharid=request.getParameter("adharcard");
		Integer adhar=Integer.parseInt(adharid);
		
		String govtrecognizedid=request.getParameter("govtrecognizedid");
		Integer govid=Integer.parseInt(govtrecognizedid);
		
		
		/*Address*/
		String country=request.getParameter("country");
		String state=request.getParameter("stateprovince");
		String address=request.getParameter("address");
		String city=request.getParameter("city");
		
		String userfax=request.getParameter("fax");
		Integer fax=Integer.parseInt(userfax);
		
		String pincode=request.getParameter("pinpostal");
		Integer pin=Integer.parseInt(pincode);
		
		/*highschool*/
		String highschool=request.getParameter("currentmostschool");
		String schoollocation=request.getParameter("schoollocation");
		String schooltype=request.getParameter("schooltype");
		String hallticketnumber=request.getParameter("hallticketnumber");
		String markssecured=request.getParameter("markssecured");
		Integer markssecure=Integer.parseInt(markssecured);
		String totalmarks=request.getParameter("totalmarks");
		Integer total=Integer.parseInt(totalmarks);
		
		/*Secondary College*/
		String secondarycollege=request.getParameter("currentmostsecondarycollege");
		String collegeloc=request.getParameter("collegelocation");
		String collegetype=request.getParameter("collegetype");
		String collegehallticketnumber=request.getParameter("collegehallticketnumber");
		String collegemarkssecured=request.getParameter("markssecuredcollege");
		Integer collegemarkssecure=Integer.parseInt(collegemarkssecured);
		String collegetotalmarks=request.getParameter("totalmarksincollege");
		Integer collegetotal=Integer.parseInt(collegetotalmarks);
		
		/*Bachelors*/
		String graduate=request.getParameter("currentmostsecondarybachelor");
		String graduateloc=request.getParameter("locationbachelor");
		String graduatetype=request.getParameter("schooltypebachelor");
		String graduatehallticketnumber=request.getParameter("hallticketnumberbachelor");
		String graduatemarkssecured=request.getParameter("markssecuredbachelor");
		Integer graduatemarkssecure=Integer.parseInt(graduatemarkssecured);
		String graduatetotalmarks=request.getParameter("totalmarksbachelor");
		Integer graduatetotal=Integer.parseInt(graduatetotalmarks);
		
		/*Secondary College*/
		String pgcollege=request.getParameter("currentmostcollegeuniversity");
		String pgcollegeloc=request.getParameter("locationmasters");
		String pgcollegetype=request.getParameter("collegeuniversitytype");
		String pgcollegehallticketnumber=request.getParameter("hallticketnumbermasters");
		
		String pgcollegemarkssecured=request.getParameter("markssecuredmasters");
		Integer pgcollegemarkssecure=Integer.parseInt(pgcollegemarkssecured);
		
		String pgcollegetotalmarks=request.getParameter("totalmarksmasters");
		Integer pgcollegetotal=Integer.parseInt(pgcollegetotalmarks);
		
		String institution=request.getParameter("institution");
		String department=request.getParameter("department");
		String dob=request.getParameter("dob");
		String password=request.getParameter("password");
		
		
		System.out.println(userID);
		
		User user = new User();
		
		user.setUserName(userName);
		user.setPassword(password);
		user.setUserRole(loggedUser.getUser().getUserRole());
		user.setFirstname(firstName);
		user.setMiddlename(mname);
		user.setLastname(lastName);
		user.setEmail(email);
		user.setPlaceofbirth(pbirth);
		user.setMaritalstatus(marital);
		user.setNationality(national);
		user.setMothersmaidenname(mothername);
		user.setAdharcard(adhar);
		user.setGovtrecognizedid(govid);
		
		
		/*Address*/
		user.setCountry(country);
		user.setStateprovince(state);
		user.setAddress(address);
		user.setCity(city);
		user.setFax(fax);
		user.setPinpostal(pin);
		
		
		
		
/*Highschool*/
		
		user.setCurrentmostschool(highschool);
		user.setSchoollocation(schoollocation);
		user.setSchooltype(schooltype);
		user.setHallticketnumber(hallticketnumber);
		user.setMarkssecured(markssecure);
		user.setTotalmarks(total);
		
/*Secondary College*/
		
		user.setCurrentmostsecondarycollege(secondarycollege);
		user.setCollegelocation(collegeloc);
		user.setCollegetype(collegetype);
		user.setCollegehallticketnumber(collegehallticketnumber);
		user.setMarkssecuredcollege(collegemarkssecure);
		user.setTotalmarksincollege(collegetotal);
		
		
/*Graduate*/
		
		user.setCurrentmostsecondarybachelor(graduate);
		user.setLocationbachelor(graduateloc);
		user.setSchooltypebachelor(graduatetype);
		user.setHallticketnumberbachelor(graduatehallticketnumber);
		user.setMarkssecuredbachelor(graduatemarkssecure);
		user.setTotalmarksbachelor(graduatetotal);

		
		
		/*	Post-graduate*/
		
		user.setCurrentmostcollegeuniversity(pgcollege);
		user.setLocationmasters(pgcollegeloc);
		user.setCollegeuniversitytype(pgcollegetype);
		user.setHallticketnumbermasters(pgcollegehallticketnumber);
		user.setMarkssecuredmasters(pgcollegemarkssecure);
		user.setTotalmarksmasters(pgcollegetotal);	
		
		
		
		userService.updateUser(user);
		String errorMessage = ErrorMessage.message;
		ErrorMessage.message = null;
		if(errorMessage != null) {
			ObjectError oerr =  new ObjectError(errorMessage,errorMessage);
			errors.addError(oerr);
			
		//createBinder(request,form);
		//bind(request,form);
		ModelAndView mav= new ModelAndView("userInfo", Constants.LOGIN_FORM_MODEL, user);
		mav.addObject(Constants.LOGIN_FORM_MODEL,user);
		return mav;
	}
	else {
		
		ModelAndView successmav =new ModelAndView(getSuccessView(),Constants.LOGIN_FORM_MODEL,user);
		
		successmav.addObject(user);
		return successmav;
	}
	/*public ModelAndView editProcess(HttpServletRequest request,
			HttpServletResponse response, LoginForm command) throws Exception {
		System.out.println("editProcess called");
		//LoginForm form= (LoginForm)request.getAttribute(Constants.LOGIN_FORM_MODEL);
		LoginForm form = (LoginForm) command;
		
		ModelAndView mav= new ModelAndView("userInfo", Constants.LOGIN_FORM_MODEL, form);
		mav.addObject(form);
		return mav;
	}*/
	
	/*@Override
	protected ServletRequestDataBinder createBinder(HttpServletRequest request,
			Object command) throws Exception {
		// TODO Auto-generated method stub
		ServletRequestDataBinder binder= super.createBinder(request, command);
		binder.bind(request);
		return binder;
	}*/
	
	
	/*@Override
	protected void bind(HttpServletRequest request, Object command)
			throws Exception {
		ServletRequestDataBinder binder=createBinder(request,command);
		binder.bind(request);
		// TODO Auto-generated method stub
	//	super.bind(request, command);
	}
*/

	}
	


	
	 
}
